<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Error;
use app\common\Common;
use app\admin\service\FinanceService;

// 财务类
class Finance extends Base
{
    private $service;

    public function __construct(FinanceService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染审核列表页
    public function examineIndex()
    {
        return view();
    }

    // 财务审核列表
    public function examineList()
    {
        $params = Request::param();
        $data = $this->service->getFinanceExamineList($params);

        return Common::success($data);
    }

    // 渲染充值报表页
    public function index()
    {
        return view();
    }

    // 充值统计
    public function rechargeStatistics()
    {
        $params = Request::param();
        $data = $this->service->getRechargeStatisticsData($params);

        return Common::success($data);
    }

    // 充值报表
    public function list()
    {
        $params = Request::param();
        $data = $this->service->getFinanceRechargeList($params);

        return Common::success($data);
    }

    // 渲染快手小程序订单页面
    public function kwaiAppletOrderIndex()
    {
        return view();
    }

    // 快手小程序订单列表
    public function kwaiAppletOrderList()
    {
        $params = Request::param();
        $data = $this->service->getKwaiAppletOrderList($params);

        return Common::success($data);
    }

    // 统计快手已支付订单金额
    public function statisticsKwaiPrice()
    {
        $params = Request::param();
        $data = $this->service->getKwaiStatisticsPrice($params);

        return Common::success($data);
    }

    // 批量订单同步
    // public function batchOrderReport()
    // {
    //     $params = Request::param();
    //     $data = $this->service->batchKwaiOrderReport($params);
    //     if (!empty($data['message'])) {
    //         return Common::fail($data['message']);
    //     }

    //     return Common::success();
    // }

    // 快手订单结算
    public function kwaiOrderSettlement()
    {
        $params = Request::param();
        $data = $this->service->kwaiOrderBatchSettlement($params);

        if ($data['code'] == 2) {
            return Common::fail($data['message']);
        }
        return Common::success([], $data['message']);
    }

    // 结算回调
    public function settlementCallback()
    {
        $params = Request::param();
        $log = fopen('./kwai_pay_callback.txt', 'a+');
        fwrite($log, json_encode($params).PHP_EOL);
        fclose($log);
    }

    // 分销商待处理结算列表
    public function distributionSettlementList()
    {
        $params = Request::param();
        $data = $this->service->getDistributionSettlementList($params);
        return Common::success($data);
    }

    // 处理分销商结算申请
    public function handleDistributionSettlement()
    {
        $params = Request::param();
        $data = $this->service->handleDistributionSettlementApply($params);
        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success();
    }

    // 虚拟支付提现单列表
    public function virtualWithdrawOrderRecords()
    {
        return view();
    }

    // 虚拟支付提现单列表
    public function virtualWithdrawOrderList()
    {
        $params = Request::param();
        $data = $this->service->getVirtualWithdrawOrderList($params);
        return Common::success($data);
    }

    // 提现结算列表
    public function withdraw()
    {
        return view();
    }

    // 提交结算申请
    public function withdrawAdd()
    {
        try {
            $params = Request::param();
            validate(\app\admin\validate\WithdrawOrderRecords::class)->scene('add')->check($params);
            $result = $this->service->editWithdrawOrderRecords($params,$this->getUserInfo());

            if (!$result) {
                return Common::fail();
            }

            return Common::success();
        } catch (\Exception $e) {
            return Common::fail($e->getMessage());
        }
    }

    // 广告金列表
    public function adFunds()
    {
        $params = Request::param();
        $data = $this->service->getAdFunds($params);
        return Common::success($data);
    }

    // 广告金导出
    public function exportAdFunds()
    {
        $params = Request::param();
        $this->service->exportAdFunds($params);
        exit();
    }

    // 小程序列表
    public function appletList()
    {
        $params = Request::param();
        $data = $this->service->getAppletList($params);
        return Common::success($data);
    }

    // 渲染广告金列表页
    public function adFundsIndex()
    {
        return view();
    }
}